package com.meirengu.webview.controller;

import com.meirengu.common.StatusCode;
import com.meirengu.model.Result;
import com.meirengu.webview.model.User;
import com.meirengu.webview.service.KwAydPushService;
import com.meirengu.webview.service.KwHnbPushService;
import com.meirengu.webview.service.UserInfoService;
import com.meirengu.webview.service.UserService;
import com.meirengu.webview.utils.MobileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author Mao
 * @create 2017-12-19 12:27
 */
@Controller
@RequestMapping("mobile")
public class MobileController extends BaseController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private KwHnbPushService kwHnbPushService;
    @Autowired
    private KwAydPushService kwAydPushService;
    @Autowired
    private UserService userService;

    Logger logger = LoggerFactory.getLogger(MobileController.class);


    @RequestMapping(value = "/{mobile}", method = RequestMethod.GET)
    @ResponseBody
    public Result detail(@PathVariable("mobile") String mobile) {
        long startTime = System.currentTimeMillis();
        String areaCode = MobileUtil.mobile(mobile);
        long endTime = System.currentTimeMillis();
        System.out.println("程序运行时间： " + (endTime - startTime) + "ms");

        Map<String, String> data = new HashMap<>();
        data.put("shengjiCode", "310000");
        data.put("areaCode", "310100");
        data.put("areaName", "上海市");

        return setResult(StatusCode.OK, data.toString(), StatusCode.codeMsgMap.get(StatusCode.OK));
    }


    @RequestMapping(value = "getMobile", method = RequestMethod.GET)
    @ResponseBody
    public Result getMobile(@RequestParam("id") String id) {
        String mobile = null;
        Map<String, Object> pushParam = new HashMap<>();
        pushParam.put("message", id);
        List<Map<String, Object>> pushList = kwHnbPushService.getList(pushParam);
        if (pushList != null && pushList.size() > 0) {
            for (Map<String, Object> map : pushList) {
                mobile = map.get("mobile").toString();
                //更新此条记录为发送成功状态
                int code = kwHnbPushService.updateSuccess(map.get("id").toString());
                if (code == 1) {
                    User user = userService.getByMobile(mobile);
                    userInfoService.updateLimitNum(1, "laiao", user);
                } else {
                    logger.info("重复获取");
                }
            }
        }

        return setResult(StatusCode.OK, mobile, StatusCode.codeMsgMap.get(StatusCode.OK));
    }

    @RequestMapping(value = "getHLMobile", method = RequestMethod.GET)
    @ResponseBody
    public Result getHLMobile(@RequestParam("id") String id) {
        String mobile = null;
        Map<String, Object> pushParam = new HashMap<>();
        pushParam.put("message", id);
        List<Map<String, Object>> pushList = kwAydPushService.getList(pushParam);
        if (pushList != null && pushList.size() > 0) {
            for (Map<String, Object> map : pushList) {
                mobile = map.get("mobile").toString();
                //更新此条记录为发送成功状态
                int code = kwAydPushService.updateSuccess(map.get("id").toString());
                if (code == 1) {
                    User user = userService.getByMobile(mobile);
                    userInfoService.updateLimitNum(1, "haluo", user);
                } else {
                    logger.info("重复获取");
                }
            }
        }
        return setResult(StatusCode.OK, mobile, StatusCode.codeMsgMap.get(StatusCode.OK));
    }


}
